package ch.six4rty.doodle.utils
{
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.IOErrorEvent;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.net.URLVariables;
	
	import nl.demonsters.debugger.MonsterDebugger;
	
	/**
	 * 
	 * @author tiagodias
	 * 
	 */	
	public class URLShortener extends EventDispatcher
	{
		private var _urlLoader				:URLLoader;
		private var _urlRequest				:URLRequest;
		private var _urlVariables			:URLVariables;
		private var _url					:Object;
		
		private static const BITLY_URL		:String		= "http://api.bit.ly/";
		private static const BITLY_VERSION	:String		= "2.0.1";
		private static const BITLY_FORMAT	:String		= "JSON";
			
		private var _bitlyLogin				:String		= "";
		private var _bitlyKey				:String		= "R_d7ede27f98ee7cc1dac752c439ac0a2e";
		
		/**
		 * The most useful method in this lib, shorten any URL to whatever you like.
		 * 
		 */		
		public function URLShortener()
		{
			if( _bitlyLogin == "" || _bitlyKey == "" )
			{
				throw new Error( "PLEASE GIVE ME THE NEEDED CREDENTIALS" );
			}
			else
			{
				init();
			}
			
		}
		
		/**
		 * 
		 * 
		 */		
		private function init():void
		{
			_urlLoader						= new URLLoader();
			_urlRequest						= new URLRequest();
			_urlVariables					= new URLVariables();
			
			_urlLoader.addEventListener( Event.COMPLETE, completeHandler );
			_urlLoader.addEventListener( IOErrorEvent.IO_ERROR, errorHandler );
		}
		
		/*------------------------
		INTERNAL FUNCTIONS
		------------------------*/
		
		final protected function completeHandler( e:Event ):void
		{
			_url							= _urlLoader.data;
			dispatchEvent( new Event( Event.COMPLETE ) );
		}
		
		final protected function errorHandler( e:IOErrorEvent ):void
		{
			dispatchEvent( new IOErrorEvent( IOErrorEvent.IO_ERROR, false, false, "URL SHORTENING FAILED" ) );
		}
		
		final protected function cleanUp():void
		{
			_urlLoader						= null;
			_urlRequest						= null;
			_urlVariables					= null;
			_urlLoader.removeEventListener( Event.COMPLETE, completeHandler );
			_urlLoader.removeEventListener( IOErrorEvent.IO_ERROR, errorHandler );
			
		}
		
		
		/*------------------------
		PUBLIC METHODS BASED ON THE BIT.LY API - http://code.google.com/p/bitly-api/wiki/ApiDocumentation
		------------------------*/
		
		/**
		 * 
		 * @param longURL
		 * 
		 */			
		public function shorten( longURL:String ):void
		{
			_urlRequest = new URLRequest( BITLY_URL + "shorten" );
			_urlVariables.version 				= BITLY_VERSION;
			_urlVariables.format				= BITLY_FORMAT;
			_urlVariables.longUrl				= longURL;
			_urlVariables.login					= _bitlyLogin;
			_urlVariables.apiKey				= _bitlyKey;
			_urlRequest.data					= _urlVariables;
			_urlLoader.load( _urlRequest );
		}
		
		/**
		 * 
		 * @param shortURL
		 * 
		 */		
		public function expand( shortURL:String ):void
		{
			_urlRequest = new URLRequest( BITLY_URL + "expand" );
			_urlVariables.version 				= BITLY_VERSION;
			_urlVariables.shortUrl				= shortURL;
			_urlVariables.login					= _bitlyLogin;
			_urlVariables.apiKey				= _bitlyKey;
			_urlRequest.data					= _urlVariables;
			_urlLoader.load( _urlRequest );
		}
		
		/**
		 * 
		 * @param shortURL
		 * 
		 */		
		public function info( shortURL:String ):void
		{
			_urlRequest = new URLRequest( BITLY_URL + "info" );
			_urlVariables.version 				= BITLY_VERSION;
			_urlVariables.shortUrl				= shortURL;
			_urlVariables.login					= _bitlyLogin;
			_urlVariables.apiKey				= _bitlyKey;
			_urlRequest.data					= _urlVariables;
			_urlLoader.load( _urlRequest );
		}
		
		/**
		 * 
		 * @param shortURL
		 * 
		 */		
		public function stats( shortURL:String ):void
		{
			_urlRequest = new URLRequest( BITLY_URL + "stats" );
			_urlVariables.version 				= BITLY_VERSION;
			_urlVariables.shortUrl				= shortURL;
			_urlVariables.login					= _bitlyLogin;
			_urlVariables.apiKey				= _bitlyKey;
			_urlRequest.data					= _urlVariables;
			_urlLoader.load( _urlRequest );
		}
		
		
		
		/*------------------------
		GETTERS / SETTERS FOR EXTERNAL CREDENTIALS
		------------------------*/
		
		/**
		 * 
		 * @return 
		 * 
		 */		
		public function get url():Object
		{
			return _url;
		}
		
		/**
		 * 
		 * @param value
		 * 
		 */		
		
		public function set login( value:String ):void
		{
			_bitlyLogin = value;
		}
		
		/**
		 * 
		 * @param value
		 * 
		 */		
		public function set key( value:String ):void
		{
			_bitlyKey = value;
		}
	}
}